import axios from "axios"
import qs from "qs"
import type { IPromiseShell } from "@/fetch/type"
import { requestInterceptor, responseInterceptor } from "./interceptor"

export const baseURL = import.meta.env.VITE_BASE_URL

const request = axios.create({
  baseURL,
  timeout: 6000
})




request.interceptors.request.use(requestInterceptor)

request.interceptors.response.use(responseInterceptor)

// get请求
const get = <T = any>(url: string, params?: Record<string, any>): IPromiseShell<T> => {
  return request.get(url + (params ? `?${qs.stringify(params)}` : ''))
}

// post请求
const post = <T = any>(url: string, data: Record<string, any>): IPromiseShell<T> => {
  return request.post(url, data)
}

// del请求
const deleteById = <T = any>(url: string, id: string | number): IPromiseShell<T> => {
  return request.delete(`${url}?id=${id}`)
}

//put请求
const put = <T = any>(url: string, data: Record<string, any>): IPromiseShell<T> => {
  return request.put(url, data)
}
const patch = <T = any>(url: string, data?: Record<string, any>): IPromiseShell<T> => {
  return request.patch(url, data)
}
export default {
  put,
  deleteById,
  post,
  get,
  patch
}